<template>
	<view class="customer-detail-viewport">
		<view class="banner-wrapper">
			<view class="place-holder-top">
			</view>
			<view class="place-holder-bottom"></view>
		</view>
		<view class="total-content">
			<view class="item-wrapper">
				<view class="item-val">
					{{total}}
				</view>
				<view class="item-txt">
					总订单数
				</view>
			</view>
			<view class="item-wrapper">
				<view class="item-val">
					{{totalAmount}}
				</view>
				<view class="item-txt">
					订单总金额
				</view>
			</view>
		</view>
		<view class="place-holder"></view>
		<view class="tui-order-list">
			<view v-if="listData.length" class="orders-wrapper">
				<view class="tui-order-item" v-for="(row,index) in listData" :key="index">
					<tui-list-cell :hover="false" :lineLeft="false">
						<view class="tui-goods-title">
							<view>订单号：{{row.orderId}}</view>
							<view class="tui-order-status">{{ orderStatusEnums[row.orderStatus] || '' }}</view>
						</view>
					</tui-list-cell>
					<block v-for="(item, i) in row.items" :key="i">
						<tui-list-cell padding="0">
							<view class="tui-goods-item">
								<image :src="item.productPic" class="tui-goods-img"></image>
								<view class="tui-goods-center">
									<view class="tui-goods-name">{{item.productName}}</view>
									<view class="tui-goods-attr">{{ item.userSelectSpecText }}</view>
									<view class="tui-goods-time">
										{{row.createTime.replace('T', ' ')}}
									</view>
								</view>
								<view class="tui-price-right">
									<view>¥{{item.productPrice}}</view>
									<view>x{{item.productNum}}</view>
								</view>
							</view>
						</tui-list-cell>
					</block>
					<tui-list-cell :hover="false" unlined>
						<view class="tui-goods-price">
							<view>共{{ row.items.length }}件商品 合计：</view>
							<view class="tui-size-24">￥</view>
							<view class="tui-price-large">{{ row.orderAmount }}</view>
						</view>
					</tui-list-cell>
				</view>
			</view>
			<view class="mt20" v-else>
				<tui-no-data :fixed="false" imgUrl="https://cwmall.oss-cn-shanghai.aliyuncs.com/mp-icon/img_noorder_3x.png">暂无订单</tui-no-data>
			</view>
			<uni-load-more :status="moreStatus" v-if="listData.length"></uni-load-more>
		</view>
	</view>
</template>

<script>
	import { LoadMore, orderStatusEnums } from '@/constants'
	
	export default {
		data() {
			return {
				orderStatusEnums,
				total: 0,
				totalAmount: 0,
				listData: [],
				moreStatus: LoadMore.noop
			};
		},
		onLoad(opt) {
			this.datas = {
				pagination: {
					pageNum: 1,
					pageSize: 20
				},
				curCustId: opt.customerId
			}
			this.totalAmount = opt.totalAmount || 0
			uni.showLoading()
			this.getOrders()
		},
		methods: {
			async getOrders() {
				const { state: { merchantId } = {} } = this.$store
				const { pageNum, pageSize } = this.datas.pagination
				const { success, data, total } = await this.$api.getOrders({
					customerId: this.datas.curCustId,
					merchantId,
					pageNum,
					pageSize
				})
				if (success) {
					this.total = total
					// this.listData = data
					if (pageNum === 1) {
						this.listData = data
						if (data.length < pageSize) {
							this.moreStatus = LoadMore.nomore
						}
					} else {
						if (data && data.length) {
							this.listData = this.listData.concat(data)
							this.moreStatus = LoadMore.noop
						} else {
							this.moreStatus = LoadMore.nomore
						}
					}
				}
				uni.hideLoading()
			}
		},
		onReachBottom() {
			if (![LoadMore.nomore, LoadMore.loading].includes(this.moreStatus)) {
				this.moreStatus = LoadMore.loading
				this.datas.pagination.pageNum++
				this.getOrders()
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}
	.customer-detail-viewport {
		.banner-wrapper {
			.place-holder-top {
				background-color: #e41f19;
				height: 40rpx;
				.filter-wrapper {
					display: flex;
					align-items: center;
					font-size: 22rpx;
					justify-content: center;
					color: #FFF;
				}
			}
			.place-holder-bottom {
				position: relative;
				border-radius: 14rpx;
				background-color: #fff;
				margin-top: -30rpx;
				height: 40rpx;
			}
		}
		.total-content {
			display: flex;
			justify-content: space-around;
			background: #fff;
			.item-wrapper {
				flex: 1;
				text-align: center;
				padding: 0 20rpx 30rpx;
				.item-val {
					font-size: 50rpx;
				}
				.item-txt {
					font-size: 28rpx;
					color: #b1b1b1;
					padding-top: 10rpx;
				}
			}
		}
		.place-holder {
			height: 20rpx;
			background: #fafafa;
		}
		.tui-order-list {
			.orders-wrapper {
				background: #fafafa;
			}
			.tui-order-item {
				margin-top: 20rpx;
				border-radius: 10rpx;
				overflow: hidden;
				.tui-goods-title {
					width: 100%;
					font-size: 28rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
				}
				.tui-goods-item {
					width: 100%;
					padding: 20rpx 30rpx;
					box-sizing: border-box;
					display: flex;
					justify-content: space-between;
					.tui-goods-img {
						width: 180rpx;
						height: 180rpx;
						display: block;
						flex-shrink: 0;
					}
					.tui-goods-center {
						flex: 1;
						padding: 20rpx 8rpx 0;
						box-sizing: border-box;
						.tui-goods-name {
							max-width: 310rpx;
							word-break: break-all;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 2;
							font-size: 26rpx;
							line-height: 32rpx;
						}
						
						.tui-goods-attr {
							font-size: 22rpx;
							color: #888888;
							line-height: 32rpx;
							padding-top: 20rpx;
							word-break: break-all;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 2;
						}
						
						.tui-goods-time {
							font-size: 22rpx;
							color: #888888;
							padding-top: 45rpx;
						}
					}
					.tui-price-right {
						text-align: right;
						font-size: 24rpx;
						color: #888888;
						line-height: 30rpx;
						padding-top: 20rpx;
					}
				}
				.tui-goods-price {
					width: 100%;
					display: flex;
					align-items: flex-end;
					justify-content: flex-end;
					font-size: 24rpx;
					.tui-size-24 {
						font-size: 24rpx;
						line-height: 24rpx;
					}
					.tui-price-large {
						font-size: 32rpx;
						line-height: 30rpx;
						font-weight: 500;
					}
				}
			}
		}
	}
</style>
